import cv2
import numpy as np
import os
import json
import copy

# 1920*1208 -> 1664*896

all_img_base = './new_Data/new_images'
all_img_path = os.listdir('./new_Data/new_images')
save_all_image_path = '/home/mingxuzhu/PycharmProjects/Test/new_Data/images_clip_832_448'

all_annotations_path = os.listdir('./new_Data/new_annotations')
save_all_annotations_path = '/home/mingxuzhu/PycharmProjects/Test/new_Data/annotations_clip_832_448'


# 1208,1920
def clip_images(img_path, save_image_path):
    file_name = os.listdir(img_path)
    for i, file in enumerate(file_name):
        print(i, file)
        img = cv2.imread(os.path.join(img_path, file))
        new_img = img[312::, 180:1844, :]
        H, W = new_img.shape[0], new_img.shape[1]
        new_img = cv2.resize(new_img, (W // 2, H // 2))
        cv2.imwrite(os.path.join(save_image_path, file), new_img)


# clip_images(img_path, save_image_path)


def adj_annos(anno_dir, anno_dir_clip):
    anno = os.listdir(anno_dir)
    for file_idx, anno_file in enumerate(anno):
        res = json.load(open(os.path.join(anno_dir, anno_file)))
        print("res:", len(res), res)
        new_res = []
        for img_idx, anno_info in enumerate(res):
            annotations = anno_info['annotations']
            print("old type class:", len(annotations))
            new_annotations = []
            new_annotations_dict = copy.deepcopy(anno_info)
            for class_idx, class_info in enumerate(annotations):
                class_type = class_info['class']
                # 13 {'class': 'vehicle(v)', 'height': 75.25612911659721, 'type': 'vehicle', 'veh': 'notcare', 'width': 145.73409130515665, 'x': 1189.7635650814427, 'y': 717.9195809376979}
                # 14 {'class': 'headlight(h)', 'height': 19.112667712151733, 'hl': 'headlight_r', 'type': 'headlight', 'width': 45.392585816360224, 'x': 1082.8520800665942, 'y': 735.2404360518353}
                new_class_info = copy.deepcopy(class_info)
                x1 = class_info['x']
                y1 = class_info['y']
                height = class_info['height']
                width = class_info['width']
                x2 = x1 + width
                y2 = y1 + height  # 1920*1208,1664*896
                newx1 = 0.0
                newx2 = 0.0
                newy1 = 0.0
                newy2 = 0.0
                newHeight = 0.0
                newWidth = 0.0
                if 180 <= x1 <= 1844 and 312 <= y1 <= 1208 and 180 <= x2 <= 1844 and 312 <= y2 <= 1208:
                    newx1 = x1 - 180
                    newx2 = x2 - 180
                    newy1 = y1 - 312
                    newy2 = y2 - 312
                elif (x1 < 180 and x2 < 180) or (x1 > 1844 and x2 > 1844) or (y1 < 312 and y2 < 312):
                    continue
                elif x1 < 180 and 1844 > x2 > 180 and y1 >= 312 and y2 <= 1208:
                    newx1 = 0.0
                    newx2 = x2 - 180
                    newy1 = y1 - 312
                    newy2 = y2 - 312
                elif 1844 > x1 > 180 and x2 > 1844 and y1 >= 312 and y2 <= 1208:
                    newx1 = x1 - 180
                    newx2 = 1663
                    newy1 = y1 - 312
                    newy2 = y2 - 312
                elif 180 <= x1 <= 1844 and 80 <= x2 <= 1844 and 0 < y1 < 312 and 312 < y2 < 1208:
                    newx1 = x1 - 180
                    newx2 = x2 - 180
                    newy1 = 0
                    newy2 = y2 - 312
                elif 180 <= x1 <= 1844 and 80 <= x2 <= 1844 and 312 < y1 < 1208 and y2 > 1208:
                    newx1 = x1 - 180
                    newx2 = x2 - 180
                    newy1 = y1 - 312
                    newy2 = 895
                elif 0 < x1 < 180 and 180 < x2 < 1844 and 0 < y1 < 312 and 312 < y2 < 1208:
                    newx1 = 0
                    newx2 = x2 - 180
                    newy1 = 0
                    newy2 = y2 - 312
                elif 1844 > x1 > 180 and x2 > 1844 and 0 < y1 < 312 and 312 < y2 < 1208:
                    newx1 = x1 - 180
                    newx2 = 1663
                    newy1 = 0
                    newy2 = y2 - 312
                elif 0 < x1 < 180 and 180 < x2 < 1844 and 1208 > y1 > 312 and y2 > 1208:
                    newx1 = 0
                    newx2 = x2 - 180
                    newy1 = y1 - 312
                    newy2 = 895
                elif 180 < x1 < 1844 and x2 > 1844 and 1208 > y1 > 312 and y2 > 1208:
                    newx1 = x1 - 180
                    newx2 = 1663
                    newy1 = y1 - 312
                    newy2 = 895
                else:
                    continue
                newx1 = newx1 / 2.0
                newy1 = newy1 / 2.0
                newx2 = newx2 / 2.0
                newy2 = newy2 / 2.0
                newHeight = newy2 - newy1
                newWidth = newx2 - newx1
                if class_type == 'vehicle(v)':
                    if newWidth <= 15 or newHeight <= 15:  # ignore
                        continue
                new_class_info['x'] = newx1
                new_class_info['y'] = newy1
                new_class_info['height'] = newHeight
                new_class_info['width'] = newWidth
                new_annotations.append(new_class_info)
            print("new type class:", len(new_annotations))
            new_annotations_dict['annotations'] = new_annotations
            if len(new_annotations) == 0:
                print("junk images")
                continue
            new_res.append(new_annotations_dict)
        # print("new_res:", len(new_res), new_res)
        with open(os.path.join(anno_dir_clip, anno_file), "w") as f:
            f.write(json.dumps(new_res, indent=4))

# for img_path in all_im g_path:
#     img_path = os.path.join(all_img_base, img_path)
#     clip_images(img_path, save_all_image_path)

# for anno_path in all_annotations_path:
#     anno_dir = os.path.join('./new_Data/new_annotations', anno_path)
#     print(anno_dir)
#     adj_annos(anno_dir, save_all_annotations_path)
